using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class MarketGroup : EveProperty
    {
        private int parentGroupId;
        private string marketGroupName;
        private string description;
        private int graphicId;
        private bool hasTypes;

        private MarketGroup parentGroup;
        private Graphic graphic;
        private List<Type> types;

        public MarketGroup Parent
        {
            get
            {
                if (parentGroupId != 0 && parentGroup == null)
                    parentGroup = new MarketGroup(db, parentGroupId);
                return parentGroup;
            }
        }
        public string Name
        {
            get { return marketGroupName; }
        }
        public string Description
        {
            get { return description; }
        }
        public Graphic Graphic
        {
            get
            {
                if (graphic == null)
                    graphic = new Graphic(db, graphicId);
                return graphic;
            }
        }
        public bool HasTypes
        {
            get { return hasTypes; }
        }
        public List<Type> Types
        {
            get
            {
                if (types == null)
                {
                    types = new List<Type>();
                    string query = "SELECT * FROM invTypes WHERE marketGroupID=" + Id;
                    foreach (DbDataRow row in db.ExecuteQuery(query))
                        types.Add(new Type(row));
                }
                return types;
            }
        }


        internal MarketGroup(DbDataRow dr)
            : base(dr.Database, EvePropertyType.MarketGroup, dr["marketGroupID"])
        {
            Fill(dr);
        }
        internal MarketGroup(Database db, int id)
            : base(db, EvePropertyType.MarketGroup, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM invMarketGroups WHERE marketGroupID=" + id));
        }

        private void Fill(DbDataRow dr)
        {
            parentGroupId = dr["parentGroupID"];
            marketGroupName = dr["marketGroupName"];
            description = dr["description"];
            graphicId = dr["graphicID"];
            hasTypes = dr["hasTypes"];
        }
    }
}
